package com.pet.serve.controller;

import com.pet.serve.entity.Shopcar;
import com.pet.serve.service.ShopcarService;
import com.pet.serve.request.ShopcarRequest;
import com.pet.serve.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/shopcar")
public class ShopcarController {

    private final ShopcarService shopcarService;

    @Autowired
    public ShopcarController(ShopcarService shopcarService) {
        this.shopcarService = shopcarService; // 使用构造函数注入
    }

    @GetMapping("/get")
    public ResponseEntity<ApiResponse> getShopCar(@RequestParam String username) {
        List<Shopcar> shopCarList = shopcarService.findByUsername(username);
        return ResponseEntity.ok(new ApiResponse(true, "获取购物车成功", shopCarList));
    }


    // 获取用户购物车
    @GetMapping("/byUsername")
    public ResponseEntity<List<Shopcar>> findByUsername(@RequestParam String username) {
        List<Shopcar> items = shopcarService.findByUsername(username);
        return ResponseEntity.ok(items);
    }


    @PostMapping("/add")
    public ApiResponse addToCart(@RequestBody ShopcarRequest request) {
        return shopcarService.addToCart(request);
    }

    // 更新购物车中的商品
    @PutMapping("/update")
    public ResponseEntity<Void> updateShopcarItem(@RequestParam Long id, @RequestParam Integer num, @RequestParam BigDecimal totalPrice) {
        shopcarService.updateShopcarItem(id, num, totalPrice);
        return ResponseEntity.noContent().build();
    }

    // 删除购物车中的商品
    @DeleteMapping("/delete")
    public ResponseEntity<ApiResponse> deleteShopcarItems(@RequestBody List<Long> shopcarids) {
        for (Long shopcarid : shopcarids) {
            try {
                shopcarService.deleteByShopcarid(shopcarid);
            } catch (EntityNotFoundException e) { // 假设你在服务层抛出此异常
                return ResponseEntity.badRequest().body(new ApiResponse(false, "Shopcar item not found", null, 0));
            }
        }
        return ResponseEntity.ok(new ApiResponse(true, "Shopcar items deleted successfully", null, 0));
    }



    // 清空用户购物车
    @DeleteMapping("/clear")
    public ResponseEntity<Void> deleteByUsername(@RequestParam String username) {
        shopcarService.deleteByUsername(username);
        return ResponseEntity.noContent().build();
    }

    // 查找购物车中指定商品或宠物
    @GetMapping("/findItem")
    public ResponseEntity<Shopcar> findByUsernameAndItem(@RequestParam String username, @RequestParam Long goodid, @RequestParam Long petid) {
        Shopcar item = shopcarService.findByUsernameAndItem(username, goodid, petid);
        return ResponseEntity.ok(item);
    }

    // 根据商品状态查找购物车中的商品
    @GetMapping("/byGoodsStatus")
    public ResponseEntity<List<Shopcar>> findByUsernameAndGoodsStatus(@RequestParam String username, @RequestParam String status) {
        List<Shopcar> items = shopcarService.findByUsernameAndGoodsStatus(username, status);
        return ResponseEntity.ok(items);
    }

    // 根据宠物状态查找购物车中的宠物
    @GetMapping("/byPetsStatus")
    public ResponseEntity<List<Shopcar>> findByUsernameAndPetsStatus(@RequestParam String username, @RequestParam String status) {
        List<Shopcar> items = shopcarService.findByUsernameAndPetsStatus(username, status);
        return ResponseEntity.ok(items);
    }
}
